//
//  GXImageView.swift
//  02进度视图抽取
//
//  Created by 龚欣 on 15/7/6.
//  Copyright © 2015年 gong. All rights reserved.
//

import UIKit

class GXImageView: UIImageView {

    var progress :CGFloat = 0{
        didSet{
            proView.hidden = progress == 0 || progress == 1
            proView.progress = progress
        }
    }
    
   private lazy var proView : ProgressView = {
        let p = ProgressView()
        self.addSubview(p)
        p.ff_Fill(self)
        return p
        }()
    
   private class ProgressView : UIView {
        
        var progress :CGFloat = 0 {
            didSet{
                setNeedsDisplay()
            }
        }
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            backgroundColor = UIColor.clearColor()
        }
        
        required init(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        override func drawRect(rect: CGRect) {
            
            let r = min(rect.width, rect.height) * 0.4
            let center = CGPoint(x: rect.width * 0.5, y: rect.height * 0.5)
            let path1 = UIBezierPath(ovalInRect: CGRect(x: center.x - r, y: center.y - r, width: 2 * r, height: 2 * r))
            UIColor(white: 0, alpha: 0.5).setFill()
            path1.fill()
            
            
            let startAngle = CGFloat(-M_PI_2)
            let endAngle = CGFloat(M_PI * 2) * progress + startAngle
            let path2 = UIBezierPath(arcCenter: center, radius: r, startAngle: startAngle, endAngle: endAngle, clockwise: true)
            UIColor(white: 1.0, alpha: 0.5).setFill()
            path2.addLineToPoint(center)
            path2.closePath()
            path2.fill()
            
            
        }
    }

   
}

